const _ = require("lodash");
window._ = _;
cc.Class({
    extends: cc.Component,

    properties: {
        handTilePrefab : cc.Prefab,
        winTilePrefab : cc.Prefab,
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
    },

    // use this for initialization
    onLoad: function () {
        this.tilesArr = [];
        this.handTile = [];
        this.meldCount = 0;

        this.selectedTile = null;
        this.myTurn = false;

        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEndAndCancel, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEndAndCancel, this);
    },

    onTouchStart : function (e) {
        if(e.target.isChildOf(this.node) && e.target != this.node){
            if(this.selectedTile){
                if(e.target == this.selectedTile){
                    if(this.myTurn){
                        var discardId = this.selectedTile.getComponent("BottomTile").MJId;
                        console.log(`点击出牌。MJId:${discardId}`);
    
                        Seats[2].getComponent("BottomCtrl").discard([discardId]);
                        return;
                    }else{
                        console.log(`没有轮到自己出牌`);
                    }
                }else{
                    this.selectedTile.getComponent("BottomTile").selected = false;
                }
            }

            this.selectedTile = e.target;
            this.selectedTile.getComponent("BottomTile").selected = true;
        }
    },

    onTouchMove : function (e) {
        var p = this.node.convertTouchToNodeSpaceAR(e);
        var tiles = this.node.children;
        for(var i in tiles){
            var tile = tiles[i];
            if(tile.getBoundingBox().contains(p)){
                if(tile.getComponent("BottomTile").selected){break;}

                this.selectedTile && (this.selectedTile.getComponent("BottomTile").selected = false);
                tile.getComponent("BottomTile").selected = true;
                this.selectedTile = tile;

                break;
            }
        }

        if(this.myTurn && this.selectedTile) {
            this.selectedTile.x += e.getDeltaX();
            this.selectedTile.y += e.getDeltaY();
        }
    },

    onTouchEndAndCancel : function (e) {
        var p = this.node.convertTouchToNodeSpaceAR(e);
        var selectedTile = this.selectedTile;
        if(!selectedTile){return;}

        if(this.myTurn){
            if(p.y > (this.node.height + this.handTilePrefab.data.height / 3 * 2)) {
                if(selectedTile.getComponent("BottomTile").selected){
                    var discardId = selectedTile.getComponent("BottomTile").MJId;
                    console.log(`滑动出牌。MJId:${discardId}`);

                    Seats[2].getComponent("BottomCtrl").discard([discardId])
                }
            }else{
                if(p.x < 0 || p.x > this.node.width){
                    selectedTile.getComponent("BottomTile").selected = false;
                    this.selectedTile = null;
                }else{
                    selectedTile.getComponent("BottomTile").selected = true;
                }
            }
        }else{
            if(p.x < 0 || p.x > this.node.width){
                selectedTile.getComponent("BottomTile").selected = false;
                this.selectedTile = null;
            }
        }
    },

    setPanelPos : function () {
        this.meldCount++;
        this.node.x = this.node.x + 181;
        if(this.meldCount == 1){
            this.node.x -= 65;
        }
    },

    // delTile : function (MJIds) {
    //     var cancelMoFlag = false;
    //     if(MJIds instanceof Array){
    //         for(var i in MJIds){
                
    //         }
    //     }
    // },

    moveTile : function () {
        var handTile = this.tilesArr.splice(-1, 1)[0];
            handTile.getComponent("BottomTile").change();

        this.handTile.push(handTile);
    },

    dealer : function (MJIds) {
        for(var i = 0; i < 13; i++){
            var tile = cc.instantiate(this.handTilePrefab);
                tile.getComponent("BottomTile").init(i, false, MJIds[i]);

            this.tilesArr.push(tile);
            this.node.addChild(tile);
        }

        MJIds.sort(function (a, b) {
            return a - b;
        });

        setTimeout(function() {
            for(var i in this.tilesArr){
                this.tilesArr[i].getComponent("BottomTile").showCover();
            }
        }.bind(this), 1700);

        setTimeout(function () {
            for(var i in this.tilesArr){
                this.tilesArr[i].destroy();
            }
            this.tilesArr.length = 0;

            for(var i = 0; i < 13; i++){
                var tile = cc.instantiate(this.handTilePrefab);
                    tile.getComponent("BottomTile").init(i, false, MJIds[i], true);
    
                this.tilesArr.push(tile);
                this.node.addChild(tile);
            }
        }.bind(this), 2100);
    },

    mo : function (MJIds) {
        for(var i in MJIds){
            var tile = cc.instantiate(this.handTilePrefab);
                tile.getComponent("BottomTile").init(this.node.childrenCount, true, MJIds[i]);

            this.handTile.push(tile);
            this.node.addChild(tile);
        }

        if(MJIds.length == 1){
            this.myTurn = true;
        }
    },

    discard : function (MJIds) {
        var startPos = [];

        this.myTurn = false;

        if(this.handTile.length == 0){
            if(this.selectedTile){
                var endIndex = this.selectedTile.getComponent("BottomTile").MJIndex;

                for(var i = this.tilesArr.length - 1; i >= endIndex; i--){
                    var tile = this.tilesArr[i];
                    var tileComp = tile.getComponent("BottomTile");
    
                    tileComp.move(-1);
                }

                startPos.push(this.selectedTile.convertToWorldSpace(cc.p(0, 0)));
                this.selectedTile.destroy();
                this.selectedTile = null;
            }
            // }else{
            //     //托管模式下吃碰后的出牌
            // }
        }else if(this.handTile.length == 1){
            if(this.selectedTile != this.handTile[0]){
                var handTile = this.handTile[0];
                var handTileComp = handTile.getComponent("BottomTile");
                var selectedTileComp = this.selectedTile.getComponent("BottomTile");
                var exchangeFlag = false;
                var endIndex = selectedTileComp.MJIndex;
    
                if(selectedTileComp.MJId > handTileComp.MJId){
                    for(var i = 0; i <= endIndex; i++){
                        var tile = this.tilesArr[i];
                        var tileComp = tile.getComponent("BottomTile");
    
                        if(tileComp.MJId > handTileComp.MJId && tileComp.MJId <= selectedTileComp.MJId){
                            if(!exchangeFlag){
                                handTileComp.exchange(tileComp.originPosition.clone());
                                handTileComp.MJIndex = tileComp.MJIndex;
                                exchangeFlag = true;
                            }
                            tileComp.move(1);
                        }
                    }
                }else if(selectedTileComp.MJId < handTileComp.MJId){
                    for(var i = this.tilesArr.length - 1; i >= endIndex; i--){
                        var tile = this.tilesArr[i];
                        var tileComp = tile.getComponent("BottomTile");
                        
                        if(tileComp.MJId < handTileComp.MJId && tileComp.MJId >= selectedTileComp.MJId){
                            if(!exchangeFlag){
                                handTileComp.exchange(tileComp.originPosition.clone());
                                handTileComp.MJIndex = tileComp.MJIndex;
                                exchangeFlag = true;
                            }
                            tileComp.move(-1);
                        }
                    }
                }else{
                    handTileComp.exchange(selectedTileComp.originPosition.clone());
                    handTileComp.MJIndex = endIndex;
                }

                this.tilesArr.splice(endIndex, 1);
                this.tilesArr.push(handTile);
                this.tilesArr.sort(function (a, b) {
                    return a.getComponent("BottomTile").MJIndex - b.getComponent("BottomTile").MJIndex;
                });
            }

            this.handTile.length = 0;
            startPos.push(this.selectedTile.convertToWorldSpace(cc.p(0, 0)));
            this.selectedTile.destroy();
            this.selectedTile = null;
        }else{
            for(var i in this.handTile){
                var tile = this.handTile[i];
                tile.destroy();
                startPos.push(tile.convertToWorldSpace(cc.p(0, 0)));
            }
            this.handTile.length = 0;
        }

        for(var i in startPos){
            if(startPos[i].y < this.handTilePrefab.data.height){
                startPos[i].y = this.handTilePrefab.data.height + 50;
            }
        }

        return startPos;
    },

    sort : function (act, MJIds, type) {
        switch(act){
            case "chi":
                this.setPanelPos();
                // this.delTile(_.difference(MJIds, type));
                var delCount = 0;
                _.pull(MJIds, type);
                for(var i = this.tilesArr.length - 1; i >= 0; i--){
                    var tile = this.tilesArr[i];
                    var tileComp = tile.getComponent("BottomTile");

                    if(_.indexOf(MJIds, tileComp.MJId) == -1){
                        tileComp.move(-2);
                    }else{
                        tile.destroy();
                        this.tilesArr.splice(i, 1);
                        delCount++;
                        if(delCount == MJIds.length){
                            break;
                        }
                    }
                }
                this.moveTile();

                this.myTurn = true;
                break;
            case "peng":
                this.setPanelPos();
                var delCount = 0;

                // if(this.handTile.length == 1){
                //     this.handTile[0].getComponent("BottomTile").move(-2);

                //     for(var i = this.tilesArr.length - 1; i >= 0; i--){
                //         var tile = this.tilesArr[i];
                //         var tileComp = tile.getComponent("BottomTile");
    
                //         if(tileComp.MJId != MJIds){
                //             tileComp.move(-3);
                //         }else{
                //             tile.destroy();
                //             this.tilesArr.splice(i, 1);
                //             delCount++;
                //             if(delCount == 3){
                //                 break;
                //             }
                //         }
                //     }
                // }else if(this.handTile.length == 0){
                    for(var i = this.tilesArr.length - 1; i >= 0; i--){
                        var tile = this.tilesArr[i];
                        var tileComp = tile.getComponent("BottomTile");
    
                        if(tileComp.MJId != MJIds){
                            tileComp.move(-2);
                        }else{
                            tile.destroy();
                            this.tilesArr.splice(i, 1);
                            delCount++;
                            if(delCount == 2){
                                break;
                            }
                        }
                    }
                    this.moveTile();
                // }

                this.myTurn = true;
                break;
            case "gang":
                switch (type) {
                    case 1:
                        this.setPanelPos();
                        var delCount = 0;
                        for(var i = this.tilesArr.length - 1; i >= 0; i--){
                            var tile = this.tilesArr[i];
                            var tileComp = tile.getComponent("BottomTile");

                            if(tileComp.MJId != MJIds){
                                tileComp.move(-3);
                            }else{
                                tile.destroy();
                                this.tilesArr.splice(i, 1);
                                delCount++;
                                if(delCount == 3){
                                    break;
                                }
                            }
                        }

                        // if(this.handTile.length == 1){
                        //     this.handTile[0].destroy();
                        //     this.handTile.length = 0;
                        // }
                        break;
                    case 2:
                        this.setPanelPos();
                        var delCount = 0;
                        var direction = -4;
                        var handTileMoveStep = 0;

                        if(this.handTile.length == 1){
                            var handTile = this.handTile[0];
                            var handTileComp = handTile.getComponent("BottomTile");
                            if(handTileComp.MJId == MJIds){
                                handTile.destroy();
                                this.handTile.length = 0;
                                delCount++;
                            }else{
                                for(var i = this.tilesArr.length - 1; i >= 0; i--){
                                    var tile = this.tilesArr[i];
                                    var tileComp = tile.getComponent("BottomTile");
        
                                    if(tileComp.MJId > handTileComp.MJId){
                                        tileComp.move(1, false, true);
                                        handTileMoveStep--;
                                    }
                                }

                                this.tilesArr.push(handTile);
                                handTileComp.move(handTileMoveStep, true);
                                this.handTile.length = 0;

                                this.tilesArr.sort(function (a, b) {
                                    return a.getComponent("BottomTile").MJIndex - b.getComponent("BottomTile").MJIndex;
                                });
                            }
                        }

                        if(delCount == 1){
                            direction = -3;
                        }

                        for(var i = this.tilesArr.length - 1; i >= 0; i--){
                            var tile = this.tilesArr[i];
                            var tileComp = tile.getComponent("BottomTile");

                            if(tileComp.MJId != MJIds){
                                tileComp.move(direction);
                            }else{
                                tile.destroy();
                                this.tilesArr.splice(i, 1);
                                delCount++;
                                if(delCount == 4){
                                    break;
                                }
                            }
                        }
                        break;
                    case 3:
                        if(this.handTile.length == 1){
                            var handTile = this.handTile[0];
                            var handTileComp = handTile.getComponent("BottomTile");
                            var handTileMoveStep = 0;

                            if(handTileComp.MJId == MJIds){
                                handTile.destroy();
                                this.handTile.length = 0;
                            }else{
                                for(var i = this.tilesArr.length - 1; i >= 0; i--){
                                    var tile = this.tilesArr[i];
                                    var tileComp = tile.getComponent("BottomTile");
        
                                    if(tileComp.MJId != MJIds){
                                        tileComp.move(-1);
                                    }else{
                                        tile.destroy();
                                        this.tilesArr.splice(i, 1);
                                        break;
                                    }
                                }
                                
                                for(var i = this.tilesArr.length - 1; i >= 0; i--){
                                    var tile = this.tilesArr[i];
                                    var tileComp = tile.getComponent("BottomTile");
        
                                    if(tileComp.MJId > handTileComp.MJId){
                                        tileComp.move(1);
                                        handTileMoveStep--;
                                    }else{
                                        this.tilesArr.push(handTile);
                                        handTileComp.move(handTileMoveStep - 1, true);
                                        this.handTile.length = 0;
                                        break;
                                    }
                                }

                                this.tilesArr.sort(function (a, b) {
                                    return a.getComponent("BottomTile").MJIndex - b.getComponent("BottomTile").MJIndex;
                                });
                                // handTileComp.move(-1);

                                // for(var i = this.tilesArr.length - 1; i >= 0; i--){
                                //     var tile = this.tilesArr[i];
                                //     var tileComp = tile.getComponent("BottomTile");
        
                                //     if(tileComp.MJId != MJIds){
                                //         tileComp.move(-1);
                                //     }else{
                                //         tile.destroy();
                                //         this.tilesArr.splice(i, 1);
                                //         break;
                                //     }
                                // }
                            }
                        }
                        break;
                }
                // if(type != 3){
                //     this.setPanelPos();
                // }
                // this.delTile(MJIds, 3);
                break;
        }
    },

    hu : function (handIds, moIds) {
        var childrenCount = 0;
        this.tilesArr.length = 0;
        this.handTile.length = 0;

        // this.node.x -= 5;
        // this.node.y -= 17;

        for(var i in this.node.children){
            this.node.children[i].destroy();
        }

        for(var i in handIds){
            var tile = cc.instantiate(this.winTilePrefab);
                tile.getComponent("BottomWin").init(childrenCount, false, handIds[i]);

            this.node.addChild(tile);
            childrenCount++;
        }

        for(var i in moIds){
            var tile = cc.instantiate(this.winTilePrefab);
                tile.getComponent("BottomWin").init(childrenCount, true, moIds[i]);

            this.node.addChild(tile);
            childrenCount++;
        }
    }
});
